<?php
/**
 * This file contains the class to provide email capabilities
 *
 * @copyright  2009 A Memory Tree
 * @link       http://my-trac.assembla.com/amemorytree/ticket/232
 */
/**
 * Provide the capability to send an email
 */
require_once('/home/amemoryt/public_html/beta/lib/Mailer.php');
define ('DEFAULTFROMNAME', 'A Memory Tree');
define ('DEFAULTFROMEMAIL', 'server@amemorytree.co.nz');
/**
 * This class provides centralised email capabilities
 */
class EmailModel
{
    /**
     * The mailer to use
     * @var Mailer
     */
    private $_mailer;
    /**
     * The from email
     *
     * @var string
     */
    private $_fromEmail;
    /**
     * The from name
     *
     * @var string
     */
    private $_fromName;
    /**
     * The to address
     *
     * @var string
     */
    private $_toEmail;
    /**
     * The to address
     *
     * @var string
     */
    private $_toName;
    /**
     * The subject
     *
     * @var string
     */
    private $_subject;
    /**
     * The message
     *
     * @var string
     */
    private $_message;
    /**
     * Create the Email class with a standard mailer.
     *
     * Allow a custom mailer to be passed, which is excellent for
     * testing purposes. The mailer defaults to a standard mailer
     * when one isn't provided.
     *
     * @return void
     * @param Mailer $mailerToUse
     */
    public function __construct($mailerToUse=null)
    {
        if (empty($mailerToUse)) {
            $mailerToUse = new Mailer();
        }
        $this->_mailer = $mailerToUse;
        $_fromEmail = '';
        $_fromName = '';
        $_toEmail = '';
        $_toName = '';
        $_subject = '';
        $_message = '';
    }
    /**
     * Set the email address of who this is coming from
     *
     * @return void
     * @param string $fromEmail
     */
    public function setFromEmail($fromEmail)
    {
        $this->_fromEmail = $fromEmail;
    }
    /**
     * Get the email address of who this is coming from
     *
     * Defaults to server@amemorytree.co.nz
     *
     * @return string
     */
    public function getFromEmail()
    {
        $fromEmail = $this->_fromEmail;
        if (empty($fromEmail)) {
            $fromEmail = DEFAULTFROMEMAIL;
        }
        return $fromEmail;
    }
    /**
     * Set the name of who this is coming from
     *
     * @return void
     * @param string $fromName
     */
    public function setFromName($fromName)
    {
        $this->_fromName = $fromName;
    }
    /**
     * Get the name of who this is being sent to
     *
     * Defaults to the email address of who this is coming from
     *
     * @return string
     */
    public function getFromName()
    {
        $fromName = $this->_fromName;
        if (empty($fromName)) {
            $fromName = $this->getFromEmail();
            if ($fromName === DEFAULTFROMEMAIL) {
                $fromName = DEFAULTFROMNAME;
            }
        }
        return $fromName;
    }
    /**
     * Get the name and email address of who this is coming from
     *
     * @return string
     */
    public function getFromAddress()
    {
        $fromName = $this->getFromName();
        $fromEmail = $this->getFromEmail();
        $fromAddress = "$fromName <$fromEmail>";
        if ($fromName === $fromEmail) {
            $fromAddress = $fromEmail;
        }
        return $fromAddress;
    }
    /**
     * Set the email address of who this is being sent to
     *
     * @return void
     * @param string $toEmail
     */
    public function setToEmail($toEmail)
    {
        $this->_toEmail = $toEmail;
    }
    /**
     * Get the email address of who this is being sent to
     *
     * Defaults to theteam@amemorytree.co.nz
     *
     * @return string
     */
    public function getToEmail()
    {
        $toEmail = $this->_toEmail;
        if (empty($toEmail)) {
            $toEmail = 'theteam@amemorytree.co.nz';
        }
        return $toEmail;
    }
    /**
     * Set the name of who this is being sent to
     *
     * @return void
     * @param string $toName
     */
    public function setToName($toName)
    {
        $this->_toName = $toName;
    }
    /**
     * Get the name of who this is being sent to
     *
     * Defaults to the email address this is being sent to
     *
     * @return string
     */
    public function getToName()
    {
        $toName = $this->_toName;
        if (empty($toName)) {
            $toName = $this->getToEmail();
        }
        return $toName;
    }
    /**
     * Get the name and email address of who this is being sent to
     *
     * @return string
     */
    public function getToAddress()
    {
        $toName = $this->getToName();
        $toEmail = $this->getToEmail();
        $toAddress = "$toName <$toEmail>";
        if ($toName === $toEmail) {
            $toAddress = $toEmail;
        }
        return $toAddress;
    }
    /**
     * Set the email subject
     *
     * @return void
     * @param string $subject
     */
    public function setSubject($subject)
    {
        $this->_subject = $subject;
    }
    /**
     * Get the email subject
     *
     * @return string
     */
    public function getSubject()
    {
        $subject = $this->_subject;
        return $subject;
    }
    /**
     * Set the email message
     *
     * @return void
     * @param string $message
     */
    public function setMessage($message)
    {
        $this->_message = $message;
    }
    /**
     * Get the email message
     *
     * @return string
     */
    public function getMessage()
    {
        $message = $this->_message;
        return $message;
    }
    /**
     * Send an email
     *
     * @return bool
     */
    public function send()
    {
        $mailer = $this->_mailer;
        $fromAddress = $this->getFromAddress();
        $toAddress = $this->getToAddress();
        $subject = $this->getSubject();
        $message = $this->getMessage();
        $success = $mailer->sendMessage($fromAddress, $toAddress, $subject, $message);
        return $success;
    }
}
?>
